/**
* @author:kellen
* @contact:xiekellen@hotmail.com
* @company:achivo-gz
* @created on:
* @modified on:
*/

package com.sasis.dao.ibatis;

import java.util.List;

import com.sasis.dao.ibatis.BaseDaoiBATIS;
import com.sasis.model.DelayReason;
import com.sasis.dao.DelayReasonDao;

import org.springframework.orm.ObjectRetrievalFailureException;

public class DelayReasonDaoiBatis extends BaseDaoiBATIS implements DelayReasonDao {

    /**
     * @see com.sasis.dao.DelayReasonDao#getDelayReasons(com.sasis.model.DelayReason)
     */
    public List getDelayReasons(final DelayReason delayReason) {
          return getSqlMapClientTemplate().queryForList("getDelayReasons", delayReason);
    }

    /**
     * @see com.sasis.dao.DelayReasonDao#getDelayReason(Integer id)
     */
    public DelayReason getDelayReason(Integer id) {
        DelayReason delayReason = (DelayReason) getSqlMapClientTemplate().queryForObject("getDelayReason", id);

        if (delayReason == null) {
            throw new ObjectRetrievalFailureException(DelayReason.class, id);
        }

        return delayReason;
    }

    /**
     * @see com.sasis.dao.DelayReasonDao#saveDelayReason(DelayReason delayReason)
     */    
    public void saveDelayReason(final DelayReason delayReason) {
        Integer id = delayReason.getId();
        // check for new record
        if (id == null) {
            id = (Integer) getSqlMapClientTemplate().insert("addDelayReason", delayReason);
        } else {
            getSqlMapClientTemplate().update("updateDelayReason", delayReason);
        }
        if( id == null ) {
            throw new ObjectRetrievalFailureException(DelayReason.class, id);
        }
    }

    /**
     * @see com.sasis.dao.DelayReasonDao#removeDelayReason(Integer id)
     */
    public void removeDelayReason(DelayReason delayReason) {
        getSqlMapClientTemplate().update("deleteDelayReason",  delayReason);
    }
}
